<template>
    <div class="container-common online-homework-current">
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item>在线作业</el-breadcrumb-item>
            <el-breadcrumb-item>当前作业</el-breadcrumb-item>
        </el-breadcrumb>
        <el-table
                :data="tableData"
                border
                style="width: 100%">
            <el-table-column
                    prop="homework"
                    label="作业">
            </el-table-column>
            <el-table-column
                    prop="publishTime"
                    label="布置时间">
            </el-table-column>
            <el-table-column
                    prop="expireTime"
                    label="截止时间">
            </el-table-column>
            <el-table-column
                    label="操作"
                    width="200">
                <template slot-scope="scope">
                    <el-button
                            type="text"
                            size="large"
                            @click="handleJoin(scope.row)">加入作业</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog
                title="提交作业"
                :visible.sync="submitDialogVisible"
                width="60%" class="submit-container">
            <template v-if="currentData">
                <div class="submit-container-item">
                    <label>作业要求</label>
                    <div v-if="currentData.content" v-html="currentData.content"></div>
                </div>
                <div class="submit-container-item">
                    <label>作业附件</label>
                    <el-button type="primary"
                               size="small"
                               @click="download">下载</el-button>
                </div>
                <div class="submit-container-item">
                    <label>作业上交</label>
                    <el-input class="submit-content" type="textarea" v-model="content"></el-input>
                    <el-upload
                            class="upload-container"
                            action="http://39.105.144.7:5002/api/upload"
                            :data="otherUploadData"
                            name="file"
                            :on-success="handleUploadSuccess"
                            :file-list="fileList">
                        <el-button size="small" type="primary">上传附件</el-button>
                        <!--<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>-->
                    </el-upload>
                </div>
                <div class="submit-container-item">
                    <label>小组成员</label>
                    <el-button
                            class="add-btn"
                            type="primary"
                            size="small"
                            @click="handleAdd">添加成员</el-button>
                    <el-table
                            :data="groupMemberTableData"
                            border
                            class="submit-table">
                        <el-table-column
                                prop="studentId"
                                label="学号">
                        </el-table-column>
                        <el-table-column
                                prop="name"
                                label="姓名">
                        </el-table-column>
                        <el-table-column
                                label="操作"
                                width="120">
                            <template slot-scope="scope">
                                <el-button
                                        type="text"
                                        size="large"
                                        @click="handleDelete(scope.$index)">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </div>
            </template>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancelSubmit">取 消</el-button>
                <el-button type="primary" @click="confirmSubmit">确 定</el-button>
            </span>
        </el-dialog>
        <el-dialog
                title="添加组员"
                :visible.sync="addDialogVisible"
                width="30%">
            <el-form :model="addForm" ref="addForm" label-width="60px" class="add-form">
                <el-form-item label="学号" prop="studentId">
                    <el-input v-model="addForm.studentId"></el-input>
                </el-form-item>
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="addForm.name"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancelAdd">取 消</el-button>
                <el-button type="primary" @click="confirmAdd">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import Api from '@utils/api';
    import {getDate} from '@utils/utils';
    import { mapGetters } from 'vuex';

    export default {
        name: "current",
        data () {
            return {
                tableData: [],
                currentData: null,
                submitDialogVisible: false,
                groupMemberTableData: [],
                fileList: [],
                homeworkFile: '',
                addDialogVisible: false,
                addForm: {
                    studentId: '',
                    name: ''
                },
                content: ''
            }
        },
        computed: {
            ...mapGetters([
                'getUserId',
                'getCurrentCourseId'
            ]),
            otherUploadData () {
                return {
                    type: 'homework',
                    id: this.getCurrentCourseId
                }
            }
        },
        methods: {
            handleJoin (data) {
                this.currentData = data;
                this.submitDialogVisible = true;
            },
            cancelSubmit () {
                this.submitDialogVisible = false;
                this.resetSubmit();
            },
            confirmSubmit () {
                let studentList = this.groupMemberTableData.map(item => item.studentId);
                studentList.push(this.getUserId);

                Api({
                    method: 'post',
                    url: '/submitHomework',
                    params: {
                        user_id: this.getUserId,
                        task_id: this.currentData.id,
                        student_list: JSON.stringify(studentList),
                        homework_content: this.content,
                        homework_file: this.homeworkFile
                    }
                }).then(() => {
                    this.$notify({
                        title: '提交成功！',
                        type: 'success'
                    });
                    this.submitDialogVisible = false;
                    this.resetSubmit();
                });
            },
            resetSubmit () {
                this.groupMemberTableData = [];
                this.fileList = [];
                this.homeworkFile = '';
                this.content = '';
            },
            handleAdd () {
                this.addDialogVisible = true;
            },
            download () {
                window.open('http://39.105.144.7:5002/api/download?UUID=' + this.currentData.taskFile);
            },
            handleUploadSuccess (response) {
                if (response.code === 200 && response.data) {
                    this.homeworkFile = response.data.id;
                }
                this.fileList = [response.data.filename];
            },
            handleDelete (index) {
                this.groupMemberTableData.splice(index, 1);
            },
            cancelAdd () {
                this.addDialogVisible = false;
                this.resetAdd();
            },
            confirmAdd () {
                this.groupMemberTableData.push(Object.assign({}, this.addForm));
                this.addDialogVisible = false;
                this.resetAdd();
            },
            resetAdd () {
                this.addForm = {
                    studentId: '',
                    name: ''
                };
            },
            getTableData () {
                Api({
                    method: 'get',
                    url: '/getHomeworkList',
                    params: {
                        course_id: this.getCurrentCourseId
                    }
                }).then(res => {
                    if (res.data && res.data.data && res.data.data.length) {
                        this.tableData = res.data.data.map(item => {
                            return {
                                id: item.task_id,
                                homework: item.task_name,
                                content: item.task_content || '',
                                publishTime: getDate(item.start_time),
                                expireTime: getDate(item.expire_time),
                                taskFile: item.task_file || ''
                            }
                        });
                    }
                })
            }
        },
        created () {
            this.getTableData();
        }
    }
</script>
<style type="text/scss" lang="scss">
    .online-homework-current {
        .submit-container {
            &-item {
                margin-bottom: 30px;
                position: relative;
            }
            label {
                color: #333;
                margin-right: 16px;
                display: block;
                width: 60px;
                font-weight: bold;
                margin-bottom: 10px;

            }
            .submit-table {
                width: 100%;
                display: inline-block;
                vertical-align: top;
                margin-top: 10px;
            }
        }
        .submit-content {
            margin-bottom: 10px;
        }
        .upload-container {
            display: inline-block;
        }
        .add-btn {
            position: absolute;
            right: 0;
            top: 0;
        }
    }
</style>